from django.contrib.auth import authenticate, login, logout
from cookland.account.models import User_Detail
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.sessions.models import Session


import datetime
today = datetime.datetime.now().strftime('%m%d')
if today < '0321' or today > '1221':
    default_style = 'inverno'
elif today >= '0321' and today < '0621':
    default_style = 'primavera'
elif today >= '0621' and today < '0921':
    default_style = 'estate'
else:
    default_style = 'autunno'


# Create your views here.
def index(request):
    style = request.session.get('style', default_style)
    return render_to_response('home.htm', {'style': style}, context_instance=RequestContext(request))

def view_form(request):
    style = request.session.get('style', default_style)
    next = request.GET['next']
    return render_to_response('login_form.htm', {'style': style,'user' : request.user, 'next' : next , 'error': "ATTENZIONE: e\' necessario loggarsi per vedere questa pagina!<br><br>Eseguire il Login"}, context_instance=RequestContext(request))

def login_view(request):
    if request.POST:
        username = request.POST['username']
        password = request.POST['password']
        next = request.POST['next']
        if not next:
            next = '/recipes/'
        user = authenticate(username = username, password = password)
        if user is not None:
            login(request, user)
            context = {'user': request.user}
            return HttpResponseRedirect(next)
            #return render_to_response('login_form.htm', context, context_instance=RequestContext(request))
        else:
            context = {'error': 'ERRORE: Login Errato'}
            style = request.session.get('style', default_style)
            context['style'] = style
            context['next'] = next
            return render_to_response('login_form.htm', context, context_instance=RequestContext(request))

def logout_view(request):
    logout(request)
    #return HttpResponseRedirect('/ricettario/')
    return HttpResponseRedirect('/recipes/')

@login_required
def profile(request, username):
    style = request.session.get('style', default_style)
    u = User.objects.get(username=username)
    u_details = User_Detail.objects.get(user = u)
    context = {'style': style,'user': request.user ,'username': username ,'nome': u.first_name, 'cognome': u.last_name, 'email': u.email, 'indirizzo': u_details.address, 'citta': u_details.city, 'homepage' : u_details.homepage}
    #context = {'nome': u.first_name, 'cognome': u.last_name, 'email': u.email}
    return render_to_response('profile.htm', context, context_instance=RequestContext(request))

def subscribe_form(request):
    style = request.session.get('style', default_style)
    return render_to_response('profile_form.htm', {'style': style}, context_instance=RequestContext(request))

@login_required
def edit_form(request):
    style = request.session.get('style', default_style)
    details = User_Detail.objects.get(user = request.user)
    return render_to_response('profile_form.htm', {
        'style': style,
        'user': request.user,
        'details': details
        }, context_instance=RequestContext(request))

def subscribe(request):
    if request.POST:
        username = request.POST['username']
        firstname = request.POST['nome']
        surname = request.POST['cognome']
        address = request.POST['indirizzo']
        city = request.POST['citta']
        occupation = request.POST['occupazione']
        email = request.POST['e_mail']
        msn = request.POST['msn']
        skype = request.POST['skype']
        homepage = request.POST['www']
        if request.user.is_authenticated():
            if request.user.username == username:
                u = request.user
                u.first_name = firstname
                u.last_name = surname
                u.email = email
                u.save()
                details = User_Detail.objects.get(user = u)
                details.homepage = homepage
                details.address = address
                details.city = city
                details.skype = skype
                details.msn = msn
                details.occupation = occupation
                details.save()
                return HttpResponseRedirect('/recipes/')
            else:
                return HttpResponseRedirect('/recipes/')
        else:
            password = request.POST['password']
            ripassword = request.POST['ripassword']
            if password != ripassword:
                return HttpResponseRedirect('/recipes/')
            u = User.objects.create_user(username = username, email = email, password = password)
            u.first_name = firstname
            u.last_name = surname
            u.save()
            details = User_Detail()
            details.create_new(u, homepage, address, city, skype, msn, occupation)
            details.save()
            return HttpResponseRedirect('/recipes/')

def change_style(request, style, next):
    request.session['style'] = style
    return HttpResponseRedirect(next)
    #return HttpResponseRedirect('/recipes/')
